home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / gold / sortobjs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.6 KB  |  272 lines

  1. /*
  2.  * The original copyright owners of the accompanying source code files have
  3.  * agreed to place such code into the public domain.  Accordingly, anyone
  4.  * who receives or obtains a copy of such source code is freely entitled to
  5.  * reproduce, use and otherwise exploit such code (including the right to
  6.  * make derivative works), at his/her own risk and expense, without any
  7.  * obligation or liability to the original copyright owners.
  8.  *
  9.  * We would appreciate (but do not require) that the following message be
  10.  * included in any derivative works:
  11.  *
  12.  * "Portions of this program were developed by Peter Broadwell, Rob Myers
  13.  * and Robin Schaufler while working in Silicon Valley."
  14.  *
  15.  * The accompanying source code files and related documentation materials
  16.  * are distributed on an "AS IS" basis, without any warranties or
  17.  * guarantees of any kind.  All implied warranties, including the implied
  18.  * warranties of merchantability and of fitness for any particular purpose,
  19.  * are expressly disclaimed.
  20.  */
  21. #include <stdio.h>
  22. #include "gl.h"
  23. /* #include "gl2/gl2cmds.h"
  24. /****/
  25.  
  26. #include "geom.h"
  27. #include "class.h"
  28. #include "classIds.h"
  29. #include "selectors.h"
  30. #include "mbox.h"
  31. #include "individual.h"
  32.  
  33. Matrix id = {
  34.     { 1.0, 0.0, 0.0, 0.0, },
  35.     { 0.0, 1.0, 0.0, 0.0, },
  36.     { 0.0, 0.0, 1.0, 0.0, },
  37.     { 0.0, 0.0, 0.0, 1.0, },
  38. };
  39.  
  40. extern Matrix gl_pickmat;
  41.  
  42. typedef struct {
  43.     long comp;
  44.     long num;
  45.     Matrix mat;
  46.     long x,y,z;
  47. } seg;
  48.  
  49. typedef struct {
  50.     float x,y,z,w;
  51. } xfcmd;
  52.  
  53. typedef struct {
  54.     long component;
  55.     float x,y,z,w;
  56.     float xunit[4];
  57.     long num;
  58. } fed;
  59.  
  60. typedef struct {
  61.     long component;
  62.     float unit[8];
  63.     long num;
  64. } ufed;
  65.  
  66. #define MAXSEGS 100
  67. #define FEEDSIZE 5000
  68.  
  69. static long ocount = 0;
  70. static long fcount = 0;
  71. static seg segs[MAXSEGS];
  72. static fed fedbuf[MAXSEGS];
  73.  
  74. #ifdef IRIS_3000
  75. static short rawbuf[FEEDSIZE];
  76. static char zflag;
  77. static char depthflag;
  78. static long oddpt;
  79. #endif /* IRIS_3000 */
  80.  
  81. int getfbnm();
  82. int getfbpt();
  83.  
  84. initsort()
  85. {
  86. #ifdef NOTDEF
  87.     zflag = getzbuffer();
  88.     depthflag = getdcm();
  89.     if(bindfbfunc(FBCxformpt,getfbpt) != TRUE)
  90.     fprintf(stderr,"bindfunc xformpt failed\n");
  91.     setdepth(-5000,5000);
  92.     /*
  93.     bcopy(id,gl_pickmat,sizeof(Matrix));
  94.     */
  95.     oddpt = 0;
  96. #endif
  97. }
  98.  
  99. #ifdef IRIS_3000
  100. getfbpt(count,command,string,bp)
  101.     int count;
  102.     int command;
  103.     char *string;
  104.     xfcmd *bp;
  105. {
  106.     register fed *fb = &fedbuf[fcount];
  107.     register int i;
  108.  
  109. #define FUDGE
  110. #ifdef FUDGE
  111.     if(!oddpt) {
  112.     fb->component = segs[fcount].comp;
  113.     fb->x = bp->x;
  114.     fb->y = bp->y;
  115.     fb->z = bp->z;
  116.     fb->w = bp->w;
  117.     }
  118.     else {
  119.     fb->num = fcount++;
  120.     fb->xunit[0] = bp->x;
  121.     fb->xunit[1] = bp->y;
  122.     fb->xunit[2] = bp->z;
  123.     fb->xunit[3] = bp->w;
  124.     }
  125.     for(i=0;i<count;i++) getfbword();
  126.     oddpt = !oddpt;
  127. #else
  128.     fb->component = segs[fcount].comp;
  129.     fb->num = fcount++;
  130.     fb->x = bp->x;
  131.     fb->y = bp->y;
  132.     fb->z = bp->z;
  133.     fb->w = bp->w;
  134.     for(i=0;i<count;i++) getfbword();
  135. #endif /* FUDGE */
  136. }
  137. #endif /* IRIS_3000 */
  138.  
  139. dolater(ob)
  140.     Object ob;
  141. {
  142.     register seg *s = &segs[ocount];
  143.  
  144.     s->comp = ob;
  145.     getmatrix(s->mat);
  146.     ocount++;
  147.     if(ocount >= MAXSEGS) {
  148.     fprintf(stderr,"gold: dolater off end of segments\007\n");
  149.     exit(1);
  150.     }
  151. }
  152.  
  153. dontdo(ob)
  154.     Object ob;
  155. {
  156.     register seg *s;
  157.  
  158.     for( s = &segs[0]; s<&(segs[ocount]); s++) {
  159.     if(s->comp == ob) {
  160.         s->comp = 0;
  161.         /* printf("caught one in dontdo()\n"); /* */
  162.     }
  163.     }
  164. }
  165. #ifdef IRIS_3000
  166. #else /* IRIS_3000 */
  167. void
  168. transform(Matrix m, int x, int y, int z)
  169. {
  170.     fedbuf[fcount].component = segs[fcount].comp;
  171.  
  172.     fedbuf[fcount].x = m[0][3] * 1;
  173.     fedbuf[fcount].y = m[1][3] * 1;
  174.     fedbuf[fcount].z = m[2][3] * 1;
  175.     fedbuf[fcount].w = m[3][3] * 1;
  176.   
  177.     fedbuf[fcount].xunit[0] = m[0][0] * x + m[0][1] * y + m[0][2] * z + m[0][3] * 1;
  178.     fedbuf[fcount].xunit[1] = m[1][0] * x + m[1][1] * y + m[1][2] * z + m[1][3] * 1;
  179.     fedbuf[fcount].xunit[2] = m[2][0] * x + m[2][1] * y + m[2][2] * z + m[2][3] * 1;
  180.     fedbuf[fcount].xunit[3] = m[3][0] * x + m[3][1] * y + m[3][2] * z + m[3][3] * 1;
  181.  
  182.     fedbuf[fcount].num = fcount;
  183.     fcount++;
  184. }
  185. #endif /* IRIS_3000 */
  186.  
  187. sortobjs(objid)
  188.     Object objid;
  189. {
  190.     register fed *fp;
  191.     register seg *s;
  192.     register long i;
  193.     int fedcmp();
  194.  
  195. #ifdef IRIS_3000
  196.     feedback(rawbuf,FEEDSIZE);;
  197.     for( s = &segs[0]; s<&(segs[ocount]); s++) {
  198.     loadmatrix(s->mat);
  199.     xfpti(0,0,0);
  200.     xfpti(1000,0,0);
  201.     }
  202.     i = endfeedback(rawbuf);
  203.  
  204.     fcount = 0;
  205.     parsefb(rawbuf,i,zflag,depthflag);
  206. #else /* IRIS_3000 */
  207.     for(fcount = 0, s = &segs[0]; fcount < ocount && s<&(segs[ocount]); s++) {
  208.     transform(s->mat,1000,0,0);
  209.     }
  210. #endif /* IRIS_3000 */
  211.  
  212.     for(i=0;i<ocount;i++) {
  213.     if(fedbuf[i].w != 0.0)
  214.         fedbuf[i].z = fedbuf[i].z/fedbuf[i].w;    /* perspective divide */
  215.     else
  216.         fedbuf[i].z = 100000.0;
  217.     }
  218.     qsort(fedbuf,ocount,sizeof(fedbuf[0]),fedcmp);
  219.  
  220.     makeobj(objid); {
  221.     initnames();
  222.     loadname(0);
  223.     pushname(0);
  224.     pushLongName(objid);
  225.     for(fp = fedbuf;fp<&(fedbuf[ocount]);fp++) {
  226.         loadmatrix(segs[fp->num].mat);
  227.         if(fp->component)
  228.         drawModels(fp->component,((ufed *)fp)->unit);    /* does callobj for each Model */
  229.     }
  230.     popLongName();
  231.     }
  232.     closeobj();
  233.     ocount = 0;
  234. }
  235.  
  236.  
  237. fedcmp(p1,p2)
  238.     fed *p1, *p2;
  239. {
  240.     if(p1->z < p2->z) return  1;
  241.     if(p1->z > p2->z) return -1;
  242.     return 0;
  243. }
  244.  
  245. spitseg(s)
  246.     seg *s;
  247. {
  248.     spitmat(s->mat);
  249.     printf("pt:\n   %d    %d    %d\n",s->x,s->y,s->z);
  250. }
  251.  
  252. spitfed(fp)
  253.     fed * fp;
  254. {
  255.     printf("fed:  %d  pt(   %g   %g   %g   %g )num = %d\n",
  256.            fp->component,fp->x,fp->y,fp->z,fp->w,fp->num);
  257. }
  258.  
  259. spitmat(s)
  260.     Matrix s;
  261. {
  262.     register long i,j;
  263.  
  264.     printf("mat:\n");
  265.     for(j=0;j<4;j++) {
  266.     for(i=0;i<4;i++) {
  267.         printf("  %g",s[i][j]);
  268.     }
  269.     printf("\n");
  270.     }
  271. }
  272.